﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PropertyExpression.Common;
using PropertyExpression.ControllableQuery;
using PropertyExpression.ControllableQuery.Test;
using PropertyExpression.ControllableQuery.Toolkit;

namespace ConsoleApplication2
{
    class Program
    {
        static void Main()
        {
            var modelCode = Console.ReadLine();
            var modelSequence = Console.ReadLine();

            foreach (var record in MaterializeReader(
                _ =>
                    {
                        var builder = new StringBuilder(
@"SELECT
    ModelPublication.PublicationId,
    ModelPublication.ModelCode,
    ModelPublication.ModelSequence
FROM
    ModelPublication
WHERE
    1 = 1");
                        if (_.ModelCode.HasValue) builder.Append(@"
    AND ModelPublication.ModelCode = " + _.ModelCode.ValueOrEx);
                        if (_.ModelSequence.HasValue) builder.Append(@"
    AND ModelPublication.ModelSequence = " + _.ModelSequence.ValueOrEx);
                        return builder.ToString().ToQuery(new
                                                              {
                                                                  PublicationId = default(Guid),
                                                                  ModelCode = default(NVarChar<L4000>),
                                                                  ModelSequence = default(NVarChar<L4000>)
                                                              });
                    },
                context => new
                    {
                        ModelCode = modelCode.IfNullOrEmptyToNothing().Select(_ => context.Param(_)),
                        ModelSequence = modelSequence.IfNullOrEmptyToNothing().Select(_ => context.Param(_))
                    }))
            {
                Console.WriteLine(
                    "PublicationId = '{0}', ModelCode = '{1}', ModelSequence = '{2}'",
                    record.PublicationId,
                    record.ModelCode,
                    record.ModelSequence);
            }
        }

        private static IEnumerable<TResult> MaterializeReader<T, TResult>(
            Func<T, IQuery<TResult>> func, Func<QueryContext, T> arg)
        {
            return MainTest.ExecuteReader(context => Func.Invoke(func, arg(context))).Select(reader => reader.Materialize2());
        }

        private static void CheckAllQueries()
        {
            QueryChecker.CheckAllQueries(
                MainTest.ConnectionString,
                PropertyExpression.ControllableQuery.Test.QueryCheck.MainTest.QueryResultPropertyTypeChecker,
                PropertyExpression.ControllableQuery.Test.QueryCheck.MainTest.ParamCreator,
                PropertyExpression.ControllableQuery.Test.QueryCheck.MainTest.QueryTypePredicate,
                PropertyExpression.ControllableQuery.Test.QueryCheck.MainTest.MethodInfoPredicate,
                PropertyExpression.ControllableQuery.Test.QueryCheck.MainTest.ChoiceInvocationGetter,
                typeof(Program).Assembly.GetTypes()
                );
        }
    }
}
